package com.jxstyle.modular.service;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jxstyle.core.base.BaseService;
import com.jxstyle.core.enums.UserManagerEnum;
import com.jxstyle.modular.entity.Menu;
import com.jxstyle.modular.entity.RoleMenu;
import com.jxstyle.modular.entity.User;
import com.jxstyle.modular.entity.UserRole;
import com.jxstyle.modular.mapper.UserMapper;

@Service
public class UserService extends BaseService<UserMapper, User> {

	@Autowired
	private MenuService menuService;
	@Autowired
	private UserRoleService userRoleService;
	@Autowired
	private RoleMenuService roleMenuService;

	public User getByUsernameOrMobile(String usernameOrMobile) {
		QueryWrapper<User> wrapper = new QueryWrapper<>();
		wrapper.eq("username", usernameOrMobile).or().eq("mobile", usernameOrMobile);
		return super.getOne(wrapper);
	}

	public List<Menu> getMenuListByUser(User user) {
		QueryWrapper<Menu> wrapper = new QueryWrapper<>();
		wrapper.eq("ismenu", 1);
		wrapper.orderByAsc("sort");
		// 查询全部菜单
		List<Menu> list = menuService.list(wrapper);
		if (user.getIsmanager().intValue() != UserManagerEnum.YES.getValue()) {
			int userid = user.getId();
			// 查询角色列表
			List<UserRole> userRoleList = userRoleService.listByUserid(userid);
			List<String> roleids = userRoleList.stream().map(UserRole::getRoleid).collect(Collectors.toList());
			// 查询角色菜单列表
			List<RoleMenu> roleMenuList = roleMenuService.listByRoleids(roleids);
			List<Integer> menuids = roleMenuList.stream().map(RoleMenu::getMenuid).collect(Collectors.toList());
			// 查询用户拥有的菜单列表
			List<Menu> selectMenus = (List<Menu>) menuService.listByIds(menuids);
			// 用来存储用户拥有的菜单列表和父级菜单id集合
			Set<Integer> allMenuIds = new HashSet<>();
			this.convertAllMenu(allMenuIds, list, selectMenus);
			// 移除掉未拥有权限的菜单
			Iterator<Menu> iterator = list.iterator();
			while (iterator.hasNext()) {
				Menu menu = iterator.next();
				if (!allMenuIds.contains(menu.getId())) {
					iterator.remove();
				}
			}
		}
		return list;
	}

	public List<Menu> getMenuTreeByUser(User user) {
		List<Menu> allList = this.getMenuListByUser(user);
		Menu tree = menuService.convertTree(allList);
		return tree.getChilds();
	}
	
	private void convertAllMenu(Set<Integer> allMenuIds, List<Menu> allList, List<Menu> selectMenus) {
		if (allMenuIds == null) {
			allMenuIds = new HashSet<>();
		}
		for (Menu selectMenu : selectMenus) {
			convertAllMenu(allMenuIds, allList, selectMenu);
		}
	}

	private void convertAllMenu(Set<Integer> allMenuIds, List<Menu> allList, Menu menu) {
		if (allMenuIds == null) {
			allMenuIds = new HashSet<>();
		}
		allMenuIds.add(menu.getId());
		if (menu.getPid() != 0) {
			for (Menu m : allList) {
				if (m.getId() == menu.getPid()) {
					convertAllMenu(allMenuIds, allList, m);
				}
			}
		}
	}
}
